<template>
  <div class="container">
    <h1>图片瀑布流懒加载示例</h1>
    <!-- 瀑布流组件 -->
    <waterfall-list :list="imageList" :lazy-load="false" :cross-origin="true" :delay="300"
      loading-img="https://via.placeholder.com/300x200?text=Loading..."
      error-img="https://via.placeholder.com/300x200?text=Error" :width="300" :gutter="15" @afterRender="onAfterRender">
      <template #item="{ item, url }">
        <div class="image-card">
          <!-- 使用data-src实现懒加载 -->
          <img :data-src="url" :alt="item.title" class="image" @load="onImageLoad" />
          <div class="info">
            <h3>{{ item.title }}</h3>
            <p>{{ item.description }}</p>
          </div>
        </div>
      </template>
    </waterfall-list>

    <!-- 加载更多按钮 -->
    <button class="load-more" @click="loadMoreImages" :disabled="isLoading">
      {{ isLoading ? '加载中...' : '加载更多' }}
    </button>
  </div>
</template>

<script>
import { ref } from 'vue';
import WaterfallList from '../../components/comCollection/vWaterfall.vue'; // 根据实际路径调整

export default {
  components: {
    WaterfallList
  },
  setup() {

    definePageMeta({
      layout: 'minimal',
      showHeader: false
    });
    // 模拟图片数据
    const generateImages = (count, startIndex = 0) => {
      return Array.from({ length: count }, (_, i) => ({
        id: startIndex + i,
        title: `图片 ${startIndex + i + 1}`,
        description: `这是第 ${startIndex + i + 1} 张图片的描述`,
        src: `https://picsum.photos/id/${startIndex + i + 10}/300/200`
      }));
    };

    const imageList = ref(generateImages(12));
    const isLoading = ref(false);

    // 图片加载完成回调
    const onImageLoad = (e) => {
      console.log('图片加载完成', e.target);
    };

    // 瀑布流渲染完成回调
    const onAfterRender = () => {
      console.log('瀑布流布局完成');
    };

    // 加载更多图片
    const loadMoreImages = () => {
      isLoading.value = true;
      setTimeout(() => {
        const newImages = generateImages(6, imageList.value.length);
        imageList.value = [...imageList.value, ...newImages];
        isLoading.value = false;
      }, 1000);
    };

    return {
      imageList,
      isLoading,
      onImageLoad,
      onAfterRender,
      loadMoreImages
    };
  }
};
</script>

<style scoped>
.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

h1 {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
}

.image-card {
  background: #fff;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease;
}

.image-card:hover {
  transform: translateY(-5px);
}

.image {
  width: 100%;
  height: auto;
  display: block;
  background: #f5f5f5;
  transition: opacity 0.3s ease;
}

.info {
  padding: 15px;
}

.info h3 {
  margin: 0 0 8px 0;
  font-size: 16px;
  color: #333;
}

.info p {
  margin: 0;
  font-size: 14px;
  color: #666;
}

.load-more {
  display: block;
  width: 200px;
  margin: 30px auto;
  padding: 12px 24px;
  background: #4a8cff;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
  transition: background 0.3s ease;
}

.load-more:hover {
  background: #3a7be0;
}

.load-more:disabled {
  background: #ccc;
  cursor: not-allowed;
}
</style>